package com.example.supermarket_back.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.supermarket_back.service.RegisterService;
import com.example.supermarket_back.utils.CommonUtil;
import com.example.supermarket_back.utils.constants.ErrorEnum;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/register")
public class Register {
    @Resource
    private RegisterService registerService;

    //发送邮箱验证码
    @PostMapping("/captcha")
    public JSONObject getCaptcha(@RequestBody JSONObject json) {
        String email = json.containsKey("email") ? json.getString("email") : null;
        if (email==null) {
            return CommonUtil.errorJson(ErrorEnum.E_409);
        }else {
            return registerService.sendCaptcha(email);
        }
    }

    //核对验证码进行账号注册
    @PostMapping("/form")
    public JSONObject registerForm(@RequestBody JSONObject json) {
        String email = json.containsKey("email") ? json.getString("email") : null;
        String password = json.containsKey("password") ? json.getString("password") : null;
        String captcha= json.containsKey("captcha") ? json.getString("captcha") : null;
        if (email==null||password==null||captcha==null) {
            return CommonUtil.errorJson(ErrorEnum.E_409);
        }else {
            return registerService.registerForm(email,password,captcha);
        }
    }
}
